/**
 * @Description:参数页使用，用来拼接无值参数
 */

import { deepClone, generateUUID } from '@/utils/util'

export const filterData = {
  // base
  fieldName: '',
  type: 'LIKE',
  value: '',
  isCaseSensitive: true,
  isFrom: '1',
  from: '',
  between: {
    isStartFrom: '1',
    isEndFrom: '1',
    from: '',
    to: '',
    startFrom: '',
    endFrom: '',
  },
  in: {
    values: [],
  },
  near: {
    location: {
      latitude: '0',
      longitude: '0',
      elevation: '0',
    },
    distance: '',
    units: '',
  },
  tag: {
    tags: '',
  },
  isNot: false,
  privateKey: generateUUID(),
}

export function isLe(type) {
  return (
    type === 'LE' ||
    type === 'LT' ||
    type === 'EQ' ||
    type === 'GT' ||
    type === 'GE' ||
    type === 'NE'
  )
}

export function isLike(type) {
  return type === 'LIKE' || type === 'NOTLIKE' || type === 'MATCHES' || type === 'NOTMATCHES'
}

export function isCommon(type) {
  return isLike(type) || isLe(type)
}

export function isBetween(type) {
  return type == 'BETWEEN' || type == 'NOTBETWEEN'
}

export function isIn(type) {
  return type == 'IN' || type == 'NOTIN'
}

export function isMissingvalue(type) {
  return type == 'MISSINGVALUE' || type == 'NOTMISSINGVALUE'
}

export function isNear(type) {
  return type == 'NEAR' || type == 'NOTNEAR'
}

export function isTag(type) {
  return type == 'TAGGED' || type == 'NOTAGGED'
}

export function changeQueryData(data) {
  let res = {}
  if (data && Object.keys(data).length !== 0) {
    if (Object.keys(data).includes('filters')) {
      let temp = deepClone(data.filters)
      res.filters = temp.map((item) => {
        return changeQueryData(item)
      })
    } else {
      res = getFilterData(data)
    }
  }
  return res
}

export function getFilterData(data) {
  let res = {}
  if (isCommon(data.type)) {
    res = {
      fieldName: data.fieldName,
      type: data.type,
      value: data.value,
      isCaseSensitive: data.isCaseSensitive,
    }
    if (data.isFrom == '2') {
      res.value = isLike(data.type) ? `**` : ''
    }
  } else if (isBetween(data.type)) {
    res = {
      fieldName: data.fieldName,
      type: data.type,
      from: data.between.from,
      to: data.between.to,
    }
    if (data.between.isStartFrom == '2') {
      res.from = ``
    }
    if (data.between.isEndFrom == '2') {
      res.to = ``
    }
  } else if (isIn(data.type)) {
    res = {
      fieldName: data.fieldName,
      type: data.type,
      value: data.in.values,
      isCaseSensitive: data.isCaseSensitive,
    }
    if (data.isFrom == '2') {
      res.values = ``
    }
  } else if (isMissingvalue(data.type)) {
    res = {
      fieldName: data.fieldName,
      type: data.type,
    }
  } else if (isNear(data.type)) {
    res = {
      fieldName: data.fieldName,
      type: data.type,
      location: data.near.location,
      distance: data.near.distance,
      units: data.near.units,
    }
  } else if (isTag(data.type)) {
    res = {
      fieldName: data.fieldName,
      type: data.type,
      tags: data.tag.tags ? JSON.parse(data.tag.tags) : [],
    }
  }
  return res
}

export function getFilterItem() {
  let res = filterData
  res.privateKey = generateUUID()
  return res
}

/**
 * 获取服务的resoutType
 * @param services
 * @param serviceKey
 * @returns {*[]}
 */
export function getDataModel(services, serviceKey) {
  const fields = services.find((item) => item.key === serviceKey)?.resultType
  if (!fields) {
    return []
  }
  const dataModel = []
  fields.map((field) => {
    dataModel.push({
      key: field.key,
      name: field.name,
      description: field.description,
      baseType: field.baseType,
    })
  })
  return dataModel
}

/*

export function
}
*/
